package com.ruoyi.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.api.domain.Products;
import com.ruoyi.api.domain.ShoppingCart;
import com.ruoyi.api.mapper.MerchantMapper;
import com.ruoyi.api.mapper.ProductsMapper;
import com.ruoyi.api.mapper.ShoppingCartMapper;
import com.ruoyi.api.service.IProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品
 *
 * @author 寻觅
 * @since 2025-05-30
 */
@Service
public class IProductsServiceImpl extends ServiceImpl<ProductsMapper, Products> implements IProductsService {

    @Autowired
    ProductsMapper productsMapper;

    @Autowired
    MerchantMapper merchantMapper;
    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Override
    public Products info(Long id) {
        Products products = productsMapper.selectById(id);

        String[] imageArray = products.getThumbnail().split(",");


        List<String> imageList = Arrays.stream(imageArray)
                .map(String::trim)  // 移除空格
                .map(s -> {
                    if (s.startsWith("'") && s.endsWith("'")) {
                        return s.substring(1, s.length() - 1); // 移除引号
                    }
                    return s;
                })
                .collect(Collectors.toList());
        products.setUrlList(imageList);
        return products;
    }

    @Override
    public List<Products> productsInfo(Long id, Long userId, String productName) {
        LambdaQueryWrapper<Products> queryWrapper = new LambdaQueryWrapper<>();
        if (id != null) {
            queryWrapper.eq(Products::getMerchantId, id);
        }
        if (productName != null) {
            queryWrapper.and(wrapper -> wrapper
                    .like(Products::getProductName, productName)
                    .or().like(Products::getSkuCode, productName)
                    .or().like(Products::getTags, productName));
        }


        if (userId != null) {
            List<Integer> productIds = shoppingCartMapper.selectList(
                            new LambdaQueryWrapper<ShoppingCart>()
                                    .eq(ShoppingCart::getUserId, userId)
                                    .select(ShoppingCart::getProductsId)
                    ).stream()
                    .map(ShoppingCart::getProductsId)
                    .collect(Collectors.toList());

            if (!productIds.isEmpty()) {
                queryWrapper.in(Products::getId, productIds);
            } else {
                // 如果购物车为空，直接返回空列表，避免执行不必要的查询
                return Collections.emptyList();
            }
        }

        return productsMapper.selectList(queryWrapper);
    }

    @Override
    public List<Products> listData(Products products) {
        return productsMapper.listData(products);
    }
}
